import pandas as pd
import os


def build_name(c_name):
    c_len = len(str(c_name))
    padding = 6-c_len
    padded = '0'*padding + str(c_name)
    return padded


def separate_data(separate_value, data, dst_dir):
    if not os.path.exists(dst_dir):
        os.makedirs(dst_dir)
    data_lst = data.groupby(separate_value)
    for _, ch_fin in data_lst:
        name = build_name(ch_fin[separate_value].values[0])
        file_dir = dst_dir + '/' + name + '.csv'
        ch_fin.to_csv(file_dir, index=False, encoding='utf-8')


def main():
    # ch_fin_con_acc = pd.read_table('data/CH_Fin_Consol_Acc_1618.csv', sep=';')
    # sample_column = ['公司', '简称', '上市交易所', '两网及退市', 'A股 上市码']
    # ch_fin_con_acc = ch_fin_con_acc[sample_column]
    # separate_data('公司', ch_fin_con_acc, 'folder01')
    ch_price = pd.read_table('data/CH_Price_20180201_20180304.csv', sep=';')
    sample_column = ['証券代碼', '简称', '上市交易所', 'CSRC分类-大类', 'SSE产业分类', '主要产品比重']
    ch_price = ch_price[sample_column]
    separate_data('証券代碼', ch_price, 'folder00')




if __name__ == '__main__':
    main()
